using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
	
	public			float					m_Speed 					= 1.0f;
	public			float					m_JumpForce 				= 100.0f;
	public			float					m_SecondJumpForce 			= 800.0f;
	public			bool					m_Freeze					= false;		// Sets by Switcher
	public			Camera					m_MyCamera					= null;
	
	private			bool					m_Grounded 					= true;
	private			bool					m_SecondJump				= false;
	
	// STATIC
	
	
	void Start ()
	{
		m_MyCamera = GetComponentInChildren<Camera>();
	}
	
	void Update ()
	{
		if (!m_Freeze)
			Move();
	}
	
	void OnCollisionStay(Collision _collision)
	{	
		if (_collision.gameObject.tag == "Ground")
		{
			m_Grounded = true;
			m_SecondJump = false;
		}
	}
	
	void OnCollisionExit(Collision _collision)
	{	
		if (_collision.gameObject.tag == "Ground")
		{
			m_Grounded = false;
		}	
	}
		
	void Move()
	{
		if ((Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)))
		{	
	        rigidbody.MovePosition(rigidbody.position + Vector3.right * m_Speed * Time.deltaTime);
		}
		
		if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)))
		{	
	        rigidbody.MovePosition(rigidbody.position - Vector3.right * m_Speed * Time.deltaTime);
		}
		
		// Jump
	    if ((Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)))
		{	
			if (m_Grounded)
			{
				rigidbody.AddForce(Vector3.up * m_JumpForce);
			}
			
			if (!m_Grounded && !m_SecondJump)
			{
				rigidbody.AddForce(Vector3.up * m_SecondJumpForce);
				m_SecondJump = true;
			}
		}
	}
}
